﻿#region Copyright (c) Nicola Gallo All rights reserved.
/*
 * All rights reserved. Reproduction of trasmission of this file, or a portion thereof,
 * is forbidden without prior written permission of Nicola Gallo.
 * 
 * Author: Nicola Gallo
 * Date: 09/04/2011
 * 
 * */
#endregion
namespace PrismMVC.Shell.Data
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Defines a view data item.
    /// </summary>
    public class ViewDataItem : INotifyPropertyChanged
    {
        //Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        //Fields

        private string _key;
        public object _value;

        //Constructors

        /// <summary>
        /// Creates a new instance of <see cref="ViewDataItem"/>.
        /// </summary>
        /// <param name="key">View data item's key.</param>
        /// <param name="value">View data item's value.</param>
        public ViewDataItem(string key, object value)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Key parameter can not be euquals to either null or empty string.");
            }
            this.Key = key;
            this.Value = value;
        }

        //Properties

        /// <summary>
        /// Gets the key.
        /// </summary>
        public string Key
        {
            get 
            {
                return this._key;
            }
            private set 
            {
                this._key = value;
                this.OnPropertyChanged("Key");
            }
        }

        /// <summary>
        /// Gets/Sets the value.
        /// </summary>
        public object Value
        {
            get 
            {
                return this._value;
            }
            set 
            {
                this._value = value;
                this.OnPropertyChanged("Value");
            } 
        }

        //Methods

        /// <summary>
        /// Serves as a hash function for <see cref="ViewDataItem"/> type.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Key.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="ViewDataItem"/> is equals to the current <see cref="ViewDataItem"/>.
        /// </summary>
        /// <param name="obj">Instance.</param>
        /// <returns>True whether the input instance is equals to the current instance otherwise False.</returns>
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }
            else if ((obj == null) || (obj.GetType() != this.GetType()))
            {
                return false;
            }
            ViewDataItem instance = obj as ViewDataItem;
            return instance.ToString().Equals(this.ToString());
        }

        /// <summary>
        /// Raises the <see cref="PresentationModel.PropertyChanged"/> event for the input property name.
        /// </summary>
        /// <param name="propertyName"></param>
        protected void OnPropertyChanged(String propertyName)
        {
            if (!string.IsNullOrWhiteSpace(propertyName))
            {
                PropertyChangedEventHandler propertyChangedHnd = this.PropertyChanged as PropertyChangedEventHandler;
                if (propertyChangedHnd != null)
                {
                    propertyChangedHnd(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}